package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 发布视频
     *  请求方式：Post
     *  参数（文件）：
     *          videoThumbnail：封面图片
     *          videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail,
                                      MultipartFile videoFile) throws IOException {
        smallVideosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询视频列表
     *  请求方式：get
     *  返回值：
     */
    @GetMapping
    public ResponseEntity queryVideoList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pr = smallVideosService.queryVideoList(page,pagesize);
        return ResponseEntity.ok(pr);
    }
    /**
     * 小视频点赞
     * 接口路径：POST/smallVideos/:id/like
     * 参数 视频id
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId){
      Integer count = smallVideosService.like(videoId);
      return ResponseEntity.ok(count);
    }
    /**
     * 小视频取消点赞
     * 接口路径：POST/smallVideos/:id/dislike
     * 视频 id
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId){
        Integer count = smallVideosService.dislike(videoId);
        return ResponseEntity.ok(count);
    }
    /**
     * 查看视频评论列表
     * 接口路径：GET/smallVideos/:id/comments
     * 视频id page pagesize
     * 返回值 PageResult
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")  Integer pagesize,
                                  @PathVariable("id") String videoId){
      PageResult result =  smallVideosService.findAllComment(page,pagesize,videoId);
        return ResponseEntity.ok(result);
    }
    /**
     * 发布视频评论
     * 接口路径：POST/smallVideos/:id/comments
     * 视频id comment
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String videoId,
                                   @RequestBody Map map ){
        String comment = (String) map.get("comment");
        smallVideosService.saveComments(videoId,comment);
       return ResponseEntity.ok(null);
    }
    /**
     * 视频评论点赞
     * 接口路径：POST/smallVideos/comments/:id/like
     * 参数 评论id
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String commentId){
        Integer count = smallVideosService.commentsLike(commentId);
        return ResponseEntity.ok(count);
    }
    /**
     * 视频评论取消点赞
     * 接口路径：POST/smallVideos/comments/:id/dislike
     * 参数 评论id
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDislike(@PathVariable("id") String commentId){
        Integer count =  smallVideosService.commentsDislike(commentId);
        return ResponseEntity.ok(count);
    }

}
